/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.hyperledger.common;

/**
 * A hierarchical and deterministical key generator following BIP32 @Link https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
 */
public interface MasterKey<K extends Key> {
    /**
     * Get the master EC key. This is the half of the generators entropy
     *
     * @return master key
     */
    K getMaster();

    /**
     * Get the additional entropy used to derive further keys from master
     *
     * @return
     */
    byte[] getChainCode();

    /**
     * This MasterKey might have been generated by an other MasterKey, if so its depth is one higher than that of the generator.
     *
     * @return generation depth. 0 is first
     */
    int getDepth();

    /**
     * Return a digest of the MasterKey that generated this key. The data might be useful to link keys if received independently
     *
     * @return
     */
    int getParent();

    /**
     * A MasterKey can genarate many other MasterKeys, this is the sequence number within siblings.
     *
     * @return sequence number within sibling MasterKeys
     */
    int getSequence();

    /**
     * A short digest of this MasterKey useful to refer to it by its children
     *
     * @return
     */
    int getFingerPrint();

    /**
     * Generate an EC key with the given sequence number. The generation is deterministic.
     *
     * @param sequence sequence number
     * @return an EC key
     * @throws HyperLedgerException - there are extremerly seldom (means won't happen in human scale) conditions where
     *                              a key can not be derived with the sequence number. Use an other. And more importantly record the exact condition this
     *                              happened as cryptographers could get rather excited of a concrete demonstration of such condition in a 256 bit key space.
     */
    K getKey(int sequence) throws HyperLedgerException;

    /**
     * Generate child MasterKey capable of further generations.
     *
     * @param sequence sequence number
     * @return a MasterKey
     * @throws HyperLedgerException - there are extremerly seldom (means won't happen in human scale) conditions where
     *                              a key can not be derived with the sequence number. Use an other. And more importantly record the exact condition this
     *                              happened as cryptographers could get rather excited of a concrete demonstration of such condition in a 256 bit key space.
     */
    MasterKey getChild(int sequence) throws HyperLedgerException;

    /**
     * BIP32 compliant serialization
     *
     * @param production - serialization will start with x if true otherwise with t
     * @return a srting in Base58 suitable to re-create this master key
     */
    String serialize(boolean production);
}
